package com.huaxinzhi.print;

import com.gprinter.command.EscCommand;
import com.gprinter.command.LabelCommand;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;

public class LabelContent {


    /**
     * 标签打印测试页
     *
     * @param json json
     * @return Vector
     */
    public static Vector<Byte> getLabel(JSONObject json) {

        if (json == null) {
            return null;
        }


        LabelCommand tsc = new LabelCommand();
        // 设置标签尺寸宽高，按照实际尺寸设置 单位mm
        tsc.addSize(80, 50);
        // 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0 单位mm
        tsc.addGap(2);
        // 设置打印方向
        tsc.addDirection(LabelCommand.DIRECTION.FORWARD, LabelCommand.MIRROR.NORMAL);
        // 开启带Response的打印，用于连续打印
        tsc.addQueryPrinterStatus(LabelCommand.RESPONSE_MODE.ON);
        // 设置原点坐标
        tsc.addReference(0, 0);
        //设置浓度
        tsc.addDensity(LabelCommand.DENSITY.DNESITY4);
        // 撕纸模式开启
        tsc.addTear(EscCommand.ENABLE.ON);
        // 清除打印缓冲区
        tsc.addCls();
        // 绘制简体中文

        int offset = 30;
        int top = offset + 20;
        int start = 30;

        int textLine = 30;
        tsc.addText(start + 120, top, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "医疗废物精细化管理登记卡");


        String name = "默认参数";
        if (json.has("compName")) {
            name = json.optString("compName", "");
        }
        int top1 = top + textLine + 20;
        tsc.addText(start, top1, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "医院名称：" + name);

        String part = "默认参数";
        if (json.has("deptName")) {
            part = json.optString("deptName", "");
        }
        int top2 = top1 + textLine;
        tsc.addText(start, top2, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "科室：" + part);

        String material = "品类名称";
        if (json.has("type_name")) {
            material = json.optString("type_name", "");
        }
        int top3 = top2 + textLine;
        tsc.addText(start, top3, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "医废类别：" + material);

        String weigh = "0";
        if (json.has("weight")) {
            weigh = json.optString("weight", "");
        }
        int top4 = top3 + textLine;
        tsc.addText(start, top4, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "重量：" + weigh + "KG");

        String time = "";
        if (json.has("weight_time")) {
            String timeTemp = json.optString("weight_time", "");
            Date date = new Date(Long.parseLong(timeTemp));
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            time = format.format(date);
        }
        int top5 = top4 + textLine;
        tsc.addText(start, top5, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "时间：" + time);

//        tsc.addText(20, top + 192, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
//                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "危险  特征：毒性、易燃性");
//        tsc.addText(20, top + 224, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
//                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "固废标识码：88-162-18/111.1111111");
//        tsc.addText(start, top + 290, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
//                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "================================");
        String packageCode = "default";

        if (json.has("package_code")) {
            packageCode = json.optString("package_code", "");
        }
        //绘制二维码
        tsc.addQRCode(start + 320, top2 + 10, LabelCommand.EEC.LEVEL_L, 5, LabelCommand.ROTATION.ROTATION_0, packageCode);

        //打印繁体
        //tsc.addUnicodeText(10,32, LabelCommand.FONTTYPE.TRADITIONAL_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,"BIG5碼繁體中文字元","BIG5");
        //打印韩文
        //tsc.addUnicodeText(10,60, LabelCommand.FONTTYPE.KOREAN, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,"Korean 지아보 하성","EUC_KR");
        // 绘制图片
        //Bitmap b = BitmapFactory.decodeResource(ApplicationContext.getInstance().getResources(), R.drawable.gprinter);
        //tsc.addBitmap(10, 80, LabelCommand.BITMAP_MODE.OVERWRITE, 300, b);

        // 绘制一维条码
        //tsc.add1DBarcode(10, 500, LabelCommand.BARCODETYPE.CODE128, 100, LabelCommand.READABEL.EANBEL, LabelCommand.ROTATION.ROTATION_0, "SMARNET");
        // 打印标签
        tsc.addPrint(1, 1);
        // 打印标签后 蜂鸣器响
        tsc.addSound(2, 100);
        //开启钱箱
        tsc.addCashdrwer(LabelCommand.FOOT.F5, 255, 255);
        Vector<Byte> datas = tsc.getCommand();
        // 发送数据
        return datas;
    }
}
